﻿@using ST = EmoneyWeiXinCommon.StringTools;
@{
    ViewBag.Title = "NewsReportList";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="@ST.GetStaticFile("My97DatePicker/skin/WdatePicker.css")" rel="stylesheet" />
<script src="@ST.GetStaticFile("My97DatePicker/WdatePicker.js")"></script>

<div class="right">
    <h3>资讯数据管理</h3>
    <!--manage-->
    <div class="manage">
        <!--manage_top-->
        <div class="manage_top">
            <ul class="manage_top_ul">
            </ul>
            <div class="manage_register">
                <p id="pType">
                </p>
                <p>
                    <span>发布时间：</span>
                    <input type="text" id="txtStart" onfocus="WdatePicker()" class="manage_register_text">
                    <span class="manage_register_font">至</span><input style="display: none" id="hidid" type="text">
                    <input type="text" id="txtEnd" onfocus="WdatePicker()" class="manage_register_text">
                </p>
            </div>
            <div class="manage_register">
                <p class="manage_register_p"><span>标题：</span><input type="text" id="txtTitle" placeholder="支持模糊查询"></p>
            </div>
            <p class="manage_add">
                <a id="btnQuery">查询</a>
                <a id="btnSend" class="manage_add_font"  style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(1, ViewBag.leftid))'>推送48小时活跃用户</a>

                <span id="spGroupID"></span>
                <a id="btnSendtoVip" class="manage_add_font"  style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(1, ViewBag.leftid))'>推送给选中组别</a>
                <span id="btnyxz"></span>
                <a id="refreshToken" class="manage_add_font" style="cursor: pointer">刷新token</a>
                <a id="btnMerged" class="manage_add_font" style="cursor: pointer">合并到该栏目</a>

                <div id="selIds"></div>
            </p>
        </div>
        <!--manage_top end-->
        <p class="manage_line"></p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <th><span>选择</span></th>
                    <th><span>编号</span></th>
                    <th><span>标题</span></th>
                    <th><span>栏目</span></th>
                    <th><span>发布时间</span></th>
                    <th><span>更新时间</span></th>
                    <th><span>是否有效</span></th>
                    @*<th><span>是否推送</span></th>*@
                    <th><span>是否发布</span></th>
                    @* 
                    <th><span>阅读次数V</span></th>
                    <th><span>阅读人次V</span></th>*@
                    <th><span>操作</span></th>
                </tr>
                <tbody id="dvList"></tbody>
            </table>
            <div class="manage_page" id="manage_page">
            </div>
        </div>
        <!--manage_bottom end-->
    </div>
    <!--manage end-->
</div>

<div style="width: 100%; height: 100%; display: none; position: absolute; top: 0px; left: 0px;" id="loadDiv">
    <div id="subLoadDiv">
        <img src="@Url.Content("~/Static/Image/load.gif")" />
    </div>
</div>

<script type="text/javascript">
    var weixinid = "";
    var pageIndex = 1;
    var pageSize = 10;
    var pageCount = 1;
    var totalCount = 0;
    var txtidc = "";
    $(document).ready(function () {
        //memutype(function (result) { if (result == "1") getContentMemu(); });
       @* bindMemuType("pType", "@ST.GetTrendsFile("Report/GetMemuTypeList")", function (result) {
            if (result == "1") {
                //选择栏目
                var mvalue = queryString(window.location.href, "ntype");
                if (mvalue) {
                    $("#drpNewsType").val(mvalue);
                }*@
        createMemu();
        //    }
        //});


        $("#btnSend").click(function () {
            if (getSelectIds() == "") {
                alert("请选择要推送的报告");
                return;
            }

            $(".manage_top_ul .manage_top_A").each(function () {
                weixinid = $(this).attr("typeid");
            })

            $.ajax({
                type: "get",
                url: "@ST.GetTrendsFile("Report/TSNewsReportUser")" + "?random=" + Math.random(),
                async: true,
                data: { weixinid: weixinid, newsreportid: getSelectIds() },
                dataType: "html",
                beforeSend: function () {
                    showLoading();
                },
                complete: function () {
                    hideLoading();
                },
                success: function (result) {
                    if (result == "-99") {
                        alert("当前用户没有登录，请登录");
                        location.href = "@ST.GetTrendsFile("Account/login")";
                    }
                    else if (result == "-1") {
                        alert("参数错误");
                    }
                    else {
                        alert("当前推送了" + result + "个用户");
                        location.href = "@ST.GetTrendsFile("Report/NewsReportList?weixinid")" + weixinid;
                    }
                }
            });

        });

        $("#btnSendtoVip").click(function () {
            if (getSelectIds() == "") {
                alert("请选择要推送的报告");
                return;
            }
            var groupId = $("#groupid").val();
            if (groupId == "-1") {
                alert("请选择分组");
            }

            $(".manage_top_ul .manage_top_A").each(function () {
                weixinid = $(this).attr("typeid");
            })

            $.ajax({
                type: "get",
                url: "@ST.GetTrendsFile("Report/TSVipJiePanNews")" + "?random=" + Math.random(),
                async: true,
                data: { weixinid: weixinid, newsreportid: getSelectIds(), groupId: groupId },
                dataType: "html",
                beforeSend: function () {
                    showLoading();
                },
                complete: function () {
                    hideLoading();
                },
                success: function (result) {
                    if (result == "-99") {
                        alert("当前用户没有登录，请登录");
                        location.href = "@ST.GetTrendsFile("Account/login")";
                    }
                    else if (result == "-1") {
                        alert("参数错误");
                    }
                    else {
                        alert("当前推送了" + result + "个用户");
                        location.href = "@ST.GetTrendsFile("Report/NewsReportList?weixinid")" + weixinid;
                    }
                }
            });
        });

        $("#refreshToken").click(function () {
            $.ajax({
                type: "get",
                url: "@ST.GetTrendsFile("Report/RefreshToken")" + "?random=" + Math.random(),
                async: true,
                data: { weixinid: weixinid },
                dataType: "html",
                success: function (result) {
                    alert(result);
                }
            });
        });

        $("#btnMerged").click(function () {
            var menuType = $("#drpNewsType").val();
            if (menuType == "-1") {
                alert("请选择要合并的栏目");
                return; //请选择要合并的栏目
            }
            $.ajax({
                type: "get",
                url: "@ST.GetTrendsFile("Report/MenuReportMerge")" + "?random=" + Math.random(),
                async: true,
                data: { weixinid: weixinid, newsreportid: getSelectIds(), menutype: menuType },
                dataType: "html",
                beforeSend: function () {
                    showLoading();
                },
                complete: function () {
                    hideLoading();
                },
                success: function (result) {
                    if (result == "1") {
                        alert("操作成功");
                    } else {
                        alert("操作失败");
                    }
                }
            });

        });

        $("#btnQuery").click(function () {
            pageIndex = 1;
            pageSize = 10;
            pageCount = 1;
            totalCount = 0;
            gotoPage();
        });
    });
    function getContentMemu() {
        $(".manage_top_ul .manage_top_A").each(function () {
            weixinid = $(this).attr("typeid");
        })
        if (weixinid > 0) {
            bindDrpList("@ST.GetTrendsFile("WXUser/QueryUserGroupList")", "spGroupID", "groupid", "id", "name", function () { });
            bindMemuType("pType", "@ST.GetTrendsFile("Report/GetMemuTypeList")", function (result) {
                if (result == "1") {
                    //选择栏目
                    var mvalue = queryString(window.location.href, "ntype");
                    if (mvalue) {
                        $("#drpNewsType").val(mvalue);
                    }
                    gotoPage();
                }
            }, weixinid);
        }
    }
    //创建labs
    function createMemu() {
        $.ajax({
            type: "get",
            url: "@ST.GetTrendsFile("BaseConfiguration/WeixinUserMemu")" + "?random=" + Math.random(),
            async: true,
            data: { weixinid: "@ViewBag.weixinid" },
            dataType: "html",
            success: function (result) {
                $(".manage_top_ul").html(result);
                $(".manage_top_ul li").click(function () {
                    $(".manage_top_ul li").removeClass("manage_top_A");
                    $(this).addClass("manage_top_A");
                    //reseting();
                    $("#btnyxz").html("");
                    getContentMemu();
                })

                getContentMemu();
            }
        });
    }
    var ajaxdata = {};
    function gotoPage() {
        ajaxdata["pindex"] = pageIndex;
        ajaxdata["psize"] = pageSize;
        ajaxdata["memutype"] = $("#drpNewsType").val();
        ajaxdata["pubdate1"] = $("#txtStart").val();
        ajaxdata["pubdate2"] = $("#txtEnd").val();
        ajaxdata["title"] = $("#txtTitle").val();
        ajaxdata["weixinid"] = weixinid;
        $.ajax({
            url: "@ST.GetTrendsFile("Report/QueryNewsReportList")",
            type: "POST",
            async: true,
            data: ajaxdata,
            beforeSend: function () {
                showLoading();
            },
            complete: function () {
                hideLoading();
            },
            success: function (data) {
                if (data != "" && data != null && data != undefined) {
                    var arrData = data.split("^");
                    totalCount = arrData[0];
                    data = arrData[1];
                    var d = eval(data);
                    var html = "";
                    if (d != "" && d != null && d != undefined) {
                        for (var i = 0; i < d.length; i++) {
                            html += "<tr>";
                            html += getSelectedTd(d[i]["ID"]);
                            html += getTxtTd(d[i]["ID"]) + '<td><span style="text-align:left">' + subStraddpoint(d[i]["title"], 15) + '</span></td>' + getTxtTd(d[i]["mtitle"]) + getTxtTd(d[i]["pubDate"].replace(/T/g, " ").substring(0, 10));
                            html += getTxtTd(d[i]["updatetime"].replace(/T/g, " ").substring(0, 19))
                                 + getValidTd(d[i]["ID"], d[i]["ValidName"], d[i]["isvalid"])
                                 + getTxtTd(d[i]["PubStatusName"]);
                            html += "<td>" + getXqA(d[i]["ID"]) + getEditA(d[i]["ID"]) + "</td>";
                            html += '</tr>';
                        }
                    }
                    $("#dvList").html(html);
                    recorderSelIds();
                    PageCountHtml();
                }
                else {
                    $("#dvList").html("没有查询到相关数据！");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
            }
        });
    }

    function getSelectIds() {
        var ids = $("#selIds").html();
        return ids.substr(0, ids.length - 1);;
    }

    function recorderSelIds() {
        $("#dvList input:checkbox").click(function () {
            if ($(this).prop("checked")) {
                $("#selIds").html($("#selIds").html() + $(this).attr("typeid") + ",");
            } else {
                $("#selIds").html($("#selIds").html().replace($(this).attr("typeid") + ",", ""));
            }
        });
    }


    function getSelectedTd(id) {
        return '<td><input typeid="' + id + '" type="checkbox" id="cbNews"/></td>';
    }
    function getTxtTd(s) {
        return '<td><span>' + s + '</span></td>';
    }
    function getXqA(id) {
        var hrefvalue = "@ST.GetTrendsFile("Report/NewsReportfbDetail")" + "?id=" + id + "&pt=1&weixinid=" + weixinid;//pt=1表示资讯，否则表示素材
        return '<a class="manage_bottom_font" href="' + hrefvalue + '" target="_blank">浏览</a>';
    }
    function getEditA(id) {
        var hrefvalue = "@ST.GetTrendsFile("Report/NewsReportfbEdit")" + "?id=" + id + "&pt=1&weixinid=" + weixinid + "&leftid=@ViewBag.leftid";//pt=1表示资讯，否则表示素材
        return '<a class="manage_bottom_font blue" href="' + hrefvalue + '" style="display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(3, ViewBag.leftid))">编辑</a>';
    }
    function getTsTd(tsname) {
        return "<td><a class=\"manage_bottom_font red\" >" + tsname + "</a></td>"
    }
    function getValidTd(id, validname, isvalid) {
        return "<td><a class=\"manage_bottom_font red\" onclick=\"operate('-1','" + id + "','" + isvalid + "')\">" + validname + "</a></td>";
    }
    function subStraddpoint(str, blLen) {
        if (str.length > blLen) {
            return str.substring(0, blLen) + "...";
        }
        else return str;
    }

    function bindDrpList(url, spid, id, value, name, fn) {
        $.ajax({
            url: url,
            type: "POST",
            async: true,
            data: { weixinid: weixinid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                datatype = eval(data);
                if (datatype) {
                    $("#" + spid).html("");
                    var d = datatype;
                    var html = '<select  id="' + id + '" ><option value="-1">全部</option>';
                    for (var i = 0; i < d.length; i++) {
                        html += ' <option value="' + d[i][value] + '">' + d[i][name] + '</option>';
                    }
                    html += '</select>';
                    $("#" + spid).append(html);
                    fn("1");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }

    //编辑操作
    function operate(op, id, isvalid) {
        $.ajax({
            url: "@ST.GetTrendsFile("Report/UpdateValidNewsReport")",
            type: "POST",
            async: true,
            data: { op: op, id: id, isvalid: isvalid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                if (data == "1") {
                    gotoPage();
                    //alert("操作成功!");

                } else alert(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }

    function showLoading() {
        var margintop = (document.documentElement.clientHeight) / 2 - 20;
        var marginleft = (document.documentElement.clientWidth) / 2 - 20;
        $("#subLoadDiv").css({ "margin-top": margintop, "margin-left": marginleft });
        $("#loadDiv").show();
    }

    function hideLoading() {
        $("#loadDiv").hide();
    }

    //翻页
    function PageCountHtml() {
        pageCount = Math.ceil(totalCount / pageSize);
        if (pageCount == 0) {
            pageCount = 1;
        }
        $("#manage_page").html(PageHtml(pageIndex, pageCount));
        $("#manage_page .manage_page_right").click(function () {
            pageIndex = parseInt($("#txtPageNum").val()) + parseInt($(this).attr("tname"));
            $("#manage_page").html(PageHtml(pageIndex, pageCount));
            gotoPage();
        });
        $("#manage_page .tiaozhuan").click(function () {
            var pageNum = $("#txtPageNum").val();
            try {
                pageIndex = parseInt(pageNum);
            } catch (e) {
                alert("请输入数字");
                return false;
            }

            if (pageNum > 0 && pageNum <= pageCount) {
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                gotoPage();
            }
            else {
                alert("当前数字超出范围");
                return false;
            }
        });
    }
</script>
